﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace BloxGin
{
    public class _3DMouse
    {
        private VertexPositionColor[] vertices;
        private GraphicsDevice device;
        private BasicEffect basicEffect;
        private VertexBuffer vertexBuffer;

        public Color Colour { get; set; }


        public _3DMouse(GraphicsDevice device)
        {
            this.device = device;
            basicEffect = new BasicEffect(device);
            Colour = Color.Black;
            InitVertices();
        }

        private void InitVertices()
        {

            float size = 1f;

            vertices = new VertexPositionColor[30];

            vertices[0] = new VertexPositionColor(new Vector3(0, 0, 0), Colour);
            vertices[1] = new VertexPositionColor(Vector3.Right * size, Colour);

            vertices[2] = new VertexPositionColor(Vector3.Right * size, Colour);
            vertices[3] = new VertexPositionColor(Vector3.Up * size + Vector3.Right * size, Colour);



            //
            //
            
            vertices[4] = new VertexPositionColor(new Vector3(0, 0, 0) + Vector3.Up * size, Colour);
            vertices[5] = new VertexPositionColor(Vector3.Right * size + Vector3.Up*size, Colour);

            vertices[6] = new VertexPositionColor(Vector3.Right * size + Vector3.Up * size, Colour);
            vertices[7] = new VertexPositionColor(Vector3.Forward * size + Vector3.Right * size + Vector3.Up * size, Colour);

            vertices[8] = new VertexPositionColor(new Vector3(0, 0, 0) + Vector3.Up * size, Colour);
            vertices[9] = new VertexPositionColor(Vector3.Forward * size + Vector3.Up * size, Colour);

            vertices[10] = new VertexPositionColor(Vector3.Forward * size + Vector3.Up * size, Colour);
            vertices[11] = new VertexPositionColor(Vector3.Forward * size + Vector3.Right * size + Vector3.Up * size, Colour);
            //


            vertices[12] = new VertexPositionColor(new Vector3(0, 0, 0), Colour);
            vertices[13] = new VertexPositionColor(Vector3.Up * size, Colour);



            vertices[14] = new VertexPositionColor(Vector3.Forward * size + Vector3.Right * size, Colour);
            vertices[15] = new VertexPositionColor(Vector3.Forward * size + Vector3.Right * size + Vector3.Up * size, Colour);



            vertices[16] = new VertexPositionColor(Vector3.Right * size, Colour);
            vertices[17] = new VertexPositionColor(Vector3.Forward * size + Vector3.Right * size, Colour);

            vertices[18] = new VertexPositionColor(new Vector3(0, 0, 0), Colour);
            vertices[19] = new VertexPositionColor(Vector3.Forward * size, Colour);

            vertices[20] = new VertexPositionColor(Vector3.Forward * size, Colour);
            vertices[21] = new VertexPositionColor(Vector3.Up * size + Vector3.Forward * size, Colour);

            vertices[22] = new VertexPositionColor(Vector3.Forward * size, Colour);
            vertices[23] = new VertexPositionColor(Vector3.Forward * size + Vector3.Right * size, Colour);

        }

        public void Draw(Matrix viewMatrix, Matrix projectionMatrix, Matrix world)
        {
            basicEffect.World = world;
            basicEffect.View = viewMatrix;
            basicEffect.Projection = projectionMatrix;
            basicEffect.VertexColorEnabled = true;

            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();

                device.SetVertexBuffer(vertexBuffer);
                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vertices, 0, 12);

            }

        }

        public void Draw(Matrix viewMatrix, Matrix projectionMatrix)
        {
            Draw(viewMatrix, projectionMatrix, Matrix.Identity);
        }

        public void DrawUsingPresetEffect()
        {

            vertexBuffer = new VertexBuffer(device, typeof(VertexPositionColor), vertices.Length, BufferUsage.WriteOnly);
            device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, vertices, 0, 9);
        }
    }
}
